#ifndef __MMALIGN_H
#define __MMALIGN_H
#include "stdtype.h"

/* calculate the size of reserved area by block count */
#define MX_MMALIGN_RASIZE(bcount) ((((bcount + MX_BYTEWIDE_MASK) >> MX_BYTEWIDE_NUM) + MX_CPU_BITWIDE_MASK) & ~MX_CPU_BITWIDE_MASK)
/* calculate the data size by (block size) * (block count) */
#define MX_MMALIGN_DSIZE(bsize, bcount) (bsize * bcount)
/* calculate all size of the memory which contain the (data size) + (reserved area size) */
#define MX_MMALIGN_ALLSIZE(bsize, bcount) (MX_MMALIGN_DSIZE(bsize, bcount) + MX_MMALIGN_RASIZE(bcount))

typedef struct
{
    mx_void_pt addr;    // start address of memory space
    mx_usize_t bsize;   // memory block size
    mx_void_pt end;
    mx_usize_t bcount;  // memory blick count
    mx_void_pt iaddr;   // start address of additional information
    mx_usize_t isize;   // size of additional information
    mx_void_pt apointer;
    mx_void_pt fpointer;
} mx_mmalign_t, *mx_mmalign_pt;

mx_usize_t mx_mmalign_init (mx_mmalign_pt pmmalign, mx_void_pt addr, mx_void_pt iaddr, mx_usize_t bsize, mx_usize_t bcount);
mx_void_pt mx_mmalign_alloc (mx_mmalign_pt pmmalign);
void mx_mmalign_free (mx_mmalign_pt pmmalign, mx_void_pt addr);
void mx_mmalign_freeall (mx_mmalign_pt pmmalign);

#endif
